const User = require('../../models/user')

module.exports = (req, res) => {
  const formData = {}
  console.log(req.body);
  formData.username = req.body.username
  formData.password = req.body.password
  formData.avatar = req.body.avatar
  formData.email = req.body.email
  formData.created_time = new Date
  formData.last_modified_time = new Date
  console.log(formData);

  User.find({ username: formData.username }, (err, userDoc) => {
    if (userDoc) {
      res.status(200).json({
        err_code: 1,
        message: '用户名已存在'
      })
    } else {
      new User(formData).save((err, newUserDoc) => {
        if (err) {
          return res.status(500).json({
            err_code: 500,
            message: err.message
          })
        }
        res.status(200).json({
          err_code: 0,
          message: '注册成功'
        })
      })
    }
  })

}